package com.micro.frog.job.rest;

import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.common.Result;
import com.micro.frog.core.contants.MaGlobal;
import com.micro.frog.core.exception.MaException;
import com.micro.frog.job.model.dto.JobDTO;
import com.micro.frog.job.model.entity.Job;
import com.micro.frog.job.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 任务实现类
 *
 * @author :  ljh
 * @date : 2018/5/10
 **/
@Slf4j
@RestController
@RequestMapping(value = "/rbac/task")
public class JobController {

    @Autowired
    JobService jobService;

    @GetMapping(value = "/all")
    public Result all() {
        Result result = new Result();
        try {
            result.setFlag(true);
            result.setData(jobService.all());
        } catch (Exception e) {
            result.setCode("20000");
            result.setMsg("系统异常" + e.getMessage());
            log.error("系统异常，e={}", e);
        }
        return result;
    }

    @PostMapping(value = "/list")
    public PageResult list(@RequestBody JobDTO jobDTO) {
        PageResult result = new PageResult();
        try {
            result = jobService.listPage(jobDTO);
        } catch (Exception e) {
            result.setCode("20000");
            result.setMsg("系统异常");
            log.error("系统异常，e={}", e);
        }
        return result;
    }

    @PostMapping("/add")
    public Result add(@RequestBody Job job) {
        Result result = new Result();
        try {
            job.setCreatedBy(MaGlobal.SYSTEM);
            jobService.addTaskAndJob(job);
            result.setFlag(true);
            result.setMsg("新增成功");
        } catch (MaException e) {
            result.setFlag(false);
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("新增失败");
            log.error("新增失败 e={}", e);
        }
        return result;
    }

    @PostMapping("/delete")
    public Result deleteDict(Integer id) {
        Result result = new Result();
        try {
            jobService.delete(id);
            result.setFlag(true);
            result.setMsg("删除成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/update")
    public Result update(@RequestBody Job job) {
        Result result = new Result();
        try {
            job.setUpdatedBy(MaGlobal.SYSTEM);
            result = jobService.updateTaskAndRebuildJob(job);
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg(e.getMessage());
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/run")
    public Result triggerSchedule(@RequestParam("taskIds[]") Integer[] taskIds, String userName) {
        Result result = new Result();
        try {
            result = jobService.runTask(taskIds, userName);
        } catch (MaException e) {
            result.setFlag(false);
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }
}
